<template>
  <div class="top">
    <div class="title">{{ item.name }}</div>
    <div class="user">{{ item.manager }}</div>
    <el-dropdown @command="commandEvent">
      <span class="el-dropdown-link">
        操作<i class="el-icon-arrow-down el-icon--right" />
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="add">添加子部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!item.istree"
            command="edit"
          >查看部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!item.istree"
            command="del"
          >删除部门</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script>
import { companyDepartmentDelete } from '@/api/department'
export default {
  props: {
    item: {
      type: Object,
      //   默认值
      default: () => ({}),
      required: true
    }
  },
  methods: {
    async commandEvent(command) {
      switch (command) {
        // 添加
        case 'add':
          this.$bus.$emit('openAdd', true, this.item, 'add')
          break
        // 删除
        case 'del':
          await this.$confirm('你确定要删除该部门吗?', '温馨提示')
          await companyDepartmentDelete(this.item.id)
          this.$message.success('删除成功')
          this.$emit('getDate')
          break
        // 编辑
        case 'edit':
          this.$bus.$emit('openAdd', true, this.item, 'edit')
          break
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.top {
  display: flex;
  .title {
    flex: 1;
  }
  .user {
    width: 120px;
    margin-right: 20px;
    text-align: right;
  }
}
</style>
